package com.dennytech.tac.base;

import java.lang.ref.WeakReference;

import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.text.Html;
import android.text.TextUtils;
import android.text.method.LinkMovementMethod;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.dennytech.tac.R;

public class CFListFragment extends CFFragment {
	protected ListView listView;
	protected FrameLayout emptyView;

	protected TextView emptyTV;
	protected String defEmptyMsg;

	private WeakReference<View> loadingView;
	private WeakReference<View> errorView;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		if (savedInstanceState != null) {
			defEmptyMsg = savedInstanceState.getString("defEmptyMsg");
		}

	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		View view = inflater.inflate(R.layout.list_fragment, container, false);
		setEmptyView(view);
		return view;
	}

	protected void setEmptyView(View view) {
		listView = (ListView) view.findViewById(R.id.list);
		emptyView = (FrameLayout) view.findViewById(R.id.empty);
		listView.setEmptyView(emptyView);
	}

	protected void setEmptyMsg(String emptyMsg, boolean bServerMsg) {
		if (emptyTV == null) {
			emptyTV = (TextView) getActivity().getLayoutInflater().inflate(
					R.layout.simple_list_item_18, emptyView, false);

			emptyTV.setMovementMethod(LinkMovementMethod.getInstance());
		}

		Drawable d = getResources().getDrawable(R.drawable.speaker);
		d.setBounds(0, 0, d.getIntrinsicWidth(), d.getIntrinsicHeight());
		emptyTV.setCompoundDrawablePadding(8);
		emptyTV.setCompoundDrawables(d, null, null, null);

		if (bServerMsg && (!TextUtils.isEmpty(emptyMsg))) {
			defEmptyMsg = emptyMsg;
		} else {
			if (TextUtils.isEmpty(defEmptyMsg)) {
				defEmptyMsg = emptyMsg;
			}
		}

		if (!TextUtils.isEmpty(defEmptyMsg)) {
			emptyTV.setText(Html.fromHtml(defEmptyMsg));
		}

		if (emptyView.getChildAt(0) != emptyTV) {
			emptyView.removeAllViews();
			emptyView.addView(emptyTV);
		}
	}

	protected View getLoadingView() {
		WeakReference<View> wr = loadingView;
		View v = wr == null ? null : wr.get();
		if (v == null) {
			v = getActivity().getLayoutInflater().inflate(
					R.layout.loading_item, emptyView, false);
			loadingView = new WeakReference<View>(v);
		}
		return v;
	}

	protected View getFailedView(String msg, View.OnClickListener retry) {
		WeakReference<View> wr = errorView;
		View v = wr == null ? null : wr.get();
		if (v == null) {
			v = getActivity().getLayoutInflater().inflate(R.layout.error_item,
					emptyView, false);
			errorView = new WeakReference<View>(v);
		}
		((TextView) v.findViewById(android.R.id.text1)).setText(msg);
		View rv = v.findViewById(R.id.btn_retry);
		rv.setVisibility(retry == null ? View.GONE : View.VISIBLE);
		rv.setOnClickListener(retry);
		rv.requestFocus();
		return v;
	}

	protected View createEmptyItem() {
		View view = new View(getActivity());
		view.setLayoutParams(new ListView.LayoutParams(
				ViewGroup.LayoutParams.MATCH_PARENT, 24));
		return view;
	}
}
